package com.app.micaihu.view.login.sy

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.app.micaihu.R
import com.app.micaihu.bean.infor.UserInfor
import com.app.micaihu.configure.Config
import com.app.micaihu.configure.ConfigKeyConstant
import com.app.micaihu.manager.AppConfigManager
import com.app.micaihu.view.login.activity.*
import com.app.micaihu.view.login.dialog.ChangeMilitaryDialog
import com.app.micaihu.view.login.sy.utils.AbScreenUtils
import com.app.micaihu.view.utils.loadCircleImage
import com.app.utils.biz.Biz
import com.app.utils.biz.Biz.Companion.instance
import com.app.widget.CommonTitle
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.StringUtils
import com.chuanglan.shanyan_sdk.OneKeyLoginManager
import com.chuanglan.shanyan_sdk.tool.ShanYanUIConfig
import com.umeng.socialize.bean.SHARE_MEDIA


object ConfigUtils {

    var tvTitle: TextView? = null

    //沉浸式竖屏样式
    @SuppressLint("SetTextI18n")
    fun getCJSConfig(context: Context, forceLogin: Boolean): ShanYanUIConfig {
        /************************************************自定义控件 */
        val logBtnImgPath = ContextCompat.getDrawable(context, R.drawable.shanyan_demo_auth_bt)
        val backgruond = ContextCompat.getDrawable(context, R.drawable.shanyan_demo_auth_no_bg)
        val returnBg = ContextCompat.getDrawable(context, R.drawable.login_close)

        val topLayout = LayoutInflater.from(context).inflate(R.layout.shanyan_title, null)

        val ivBack = topLayout.findViewById<ImageView>(R.id.ivBack)
        val tvChange = topLayout.findViewById<TextView>(R.id.tvChange)
        ivBack.setOnClickListener {
            OneKeyLoginManager.getInstance().removeAllListener()
            OneKeyLoginManager.getInstance().finishAuthActivity()
            ActivityUtils.finishActivity(QuickLoginActivity::class.java)
            if (forceLogin) {
                ActivityUtils.finishAllActivities()
            }
        }
        tvChange.setOnClickListener {
            val changeMilitaryDialog = ChangeMilitaryDialog(context)
            changeMilitaryDialog.setChangeMilitaryListener(object : ChangeMilitaryDialog.OnChangeMilitaryListener {
                override fun changeMilitary(index: Int) {
                    tvTitle?.text = "欢迎来到迷彩虎" + Config.militaryList[index - 1]
                }
            })
            changeMilitaryDialog.show()
        }

        val logoLayout = LayoutInflater.from(context).inflate(R.layout.shanyan_dec, null) as RelativeLayout
        val logoParams = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT)
        logoParams.setMargins(0, AbScreenUtils.dp2px(context, 70f), 0, 0)
        logoLayout.layoutParams = logoParams
        tvTitle = logoLayout.findViewById(R.id.tvTitle)
        val military = instance.military
        if (StringUtils.isEmpty(military)) {
            tvTitle?.text = "欢迎来到迷彩虎"
        } else {
            val index = instance.military.toInt()
            tvTitle?.text = "欢迎来到迷彩虎" + Config.militaryList[index - 1]
        }
        val relativeLayout = LayoutInflater.from(context).inflate(R.layout.shanyan_other_login_layout, null) as RelativeLayout
        val layoutParamsOther = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT)
        layoutParamsOther.setMargins(0, AbScreenUtils.dp2px(context, 300f), 0, 0)
        layoutParamsOther.addRule(RelativeLayout.CENTER_HORIZONTAL)
        relativeLayout.layoutParams = layoutParamsOther
        val tvLoginCode = relativeLayout.findViewById<TextView>(R.id.tvLoginCode)
        val tvLoginAccount = relativeLayout.findViewById<TextView>(R.id.tvLoginAccount)
        tvLoginCode.setOnClickListener {
            val intent = Intent(context, PhoneLoginActivity::class.java)
            ActivityUtils.startActivity(intent)
        }
        tvLoginAccount.setOnClickListener {
            val intent = Intent(context, AccountLoginActivity::class.java)
            ActivityUtils.startActivity(intent)
        }
        val otherLogin = LayoutInflater.from(context).inflate(R.layout.login_other_login, null) as RelativeLayout
        val otherLoginParams = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT)
        otherLoginParams.setMargins(0, AbScreenUtils.getScreenHeight(context, false) - AbScreenUtils.dp2px(context, 200f), 0, 0)
        otherLogin.layoutParams = otherLoginParams
        val ivQQLogin = otherLogin.findViewById<ImageView>(R.id.ivQQLogin)
        val ivWxLogin = otherLogin.findViewById<ImageView>(R.id.ivWxLogin)
        val ivSinaLogin = otherLogin.findViewById<ImageView>(R.id.ivSinaLogin)

        ivQQLogin.setOnClickListener {
            LogUtils.e("loginQQ")
            val intent = Intent(context, PlatFormInfoActivity::class.java)
            intent.putExtra(Config.Extra.name, SHARE_MEDIA.QQ)
            ActivityUtils.startActivity(intent)
        }
        ivWxLogin.setOnClickListener {
            LogUtils.e("loginWx")
            val intent = Intent(context, PlatFormInfoActivity::class.java)
            intent.putExtra(Config.Extra.name, SHARE_MEDIA.WEIXIN)
            ActivityUtils.startActivity(intent)
        }
        ivSinaLogin.setOnClickListener {
            LogUtils.e("loginSina")
            val intent = Intent(context, PlatFormInfoActivity::class.java)
            intent.putExtra(Config.Extra.name, SHARE_MEDIA.SINA)
            ActivityUtils.startActivity(intent)
        }
        val userAgreementUrl = AppConfigManager.getInstance().configSP.getString(ConfigKeyConstant.userAgreementUrl, "")
        val privateAgreementUrl = AppConfigManager.getInstance().configSP.getString(ConfigKeyConstant.privateAgreementUrl, "")
        return ShanYanUIConfig.Builder() //                .setLightColor(true)
                .setActivityTranslateAnim("shanyan_demo_fade_in_anim", "shanyan_dmeo_fade_out_anim") //授权页导航栏：
                .setNavColor(Color.parseColor("#222222")) //设置导航栏颜色
                .setNavText("") //设置导航栏标题文字
                .setNavTextSize(18)
                .setNavTextColor(Color.parseColor("#222222")) //                .setAuthBGImgPath(backgruond)
                .setLogoHidden(true) //是否隐藏logo
                .setDialogDimAmount(0f)
                .setAuthNavHidden(true)
//                .setNavReturnImgPath(returnBg)
//                .setNavReturnBtnWidth(15)
//                .setNavReturnBtnHeight(15)
//                .setNavReturnBtnOffsetX(15)
//                .setNavReturnBtnOffsetY(10)
                .setFullScreen(false)
                .setStatusBarHidden(false) //                .setStatusBarColor(Color.parseColor(color))
                //授权页号码栏：
                .setNumberColor(Color.parseColor("#222222")) //设置手机号码字体颜色
                //                .setNumFieldOffsetBottomY(250)    //设置号码栏相对于标题栏下边缘y偏移
                //                .setNumFieldOffsetX(250)
                .setNumberSize(24)
                .setNumFieldHeight(50) //                .setNumFieldOffsetX(155)
                .setNumFieldOffsetY(170)
                .setSloganHidden(false)
                .setSloganTextSize(12)
                .setSloganOffsetY(230) //授权页登录按钮：
                .setLogBtnText("一键登录") //设置登录按钮文字
                .setLogBtnTextColor(-0x1) //设置登录按钮文字颜色
                .setLogBtnImgPath(logBtnImgPath) //设置登录按钮图片
                .setLogBtnTextSize(15)
                .setLogBtnHeight(45)
                .setLogBtnOffsetY(255)
                .setLogBtnWidth(AbScreenUtils.getScreenWidth(context, true) - 60) //授权页隐私栏：
                .setAppPrivacyOne("用户协议", userAgreementUrl) //设置开发者隐私条款1名称和URL(名称，url)
                .setAppPrivacyTwo("隐私政策", privateAgreementUrl) //设置开发者隐私条款2名称和URL(名称，url)
                .setAppPrivacyColor(Color.parseColor("#ABABAB"), Color.parseColor("#007DCD")) //	设置隐私条款名称颜色(基础文字颜色，协议文字颜色)
                .setPrivacyText("同意", "和", "、", "、", "并授权" + context.getString(R.string.app_name) + "获取手机号")
                .setPrivacyOffsetBottomY(15) //设置隐私条款相对于屏幕下边缘y偏
                .setPrivacyState(true)
                .setPrivacyTextSize(12)
                .setPrivacyOffsetX(26)
                .setCheckBoxHidden(true)
                .setShanYanSloganHidden(true)
                .setShanYanSloganTextColor(Color.parseColor("#222222"))
                .addCustomView(topLayout, false, false, null)
                .addCustomView(logoLayout, false, false, null)
                .addCustomView(relativeLayout, false, false, null)
                .addCustomView(otherLogin, false, false, null) //                .setLoadingView(view_dialog)
                .build()
    }

    //沉浸式竖屏样式
    @SuppressLint("SetTextI18n")
    fun getBindPhoneCJSConfig(context: Context, showSkip: Boolean): ShanYanUIConfig {

        /************************************************自定义控件 */
        val logBtnImgPath = ContextCompat.getDrawable(context, R.drawable.shanyan_demo_auth_bt)

        val mCommonTitle = CommonTitle(context, null)
        mCommonTitle.title = "绑定手机号"
        mCommonTitle.setVisibilityConfirm(showSkip)
        mCommonTitle.setTextConfirm("跳过")
        mCommonTitle.setCommonTitleCallBackListener(object : CommonTitle.CommonTitleCallBackListener {
            override fun btnBackOnClick() {
                OneKeyLoginManager.getInstance().removeAllListener()
                OneKeyLoginManager.getInstance().finishAuthActivity()
                ActivityUtils.finishActivity(BindPhoneActivity::class.java)
            }

            override fun btnConfirmOnClick() {
                OneKeyLoginManager.getInstance().removeAllListener()
                OneKeyLoginManager.getInstance().finishAuthActivity()
                ActivityUtils.finishActivity(BindPhoneActivity::class.java)
            }

        })

        val headLayout = LayoutInflater.from(context).inflate(R.layout.shanyan_bind_head_layout, null) as RelativeLayout
        val layoutParamsHead = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT)
        layoutParamsHead.setMargins(0, AbScreenUtils.dp2px(context, 80f), 0, 0)
        layoutParamsHead.addRule(RelativeLayout.CENTER_HORIZONTAL)
        headLayout.layoutParams = layoutParamsHead
        val ivHead = headLayout.findViewById<ImageView>(R.id.ivHead)
        val tvNickName = headLayout.findViewById<TextView>(R.id.tvNickName)
        val userInfoStr = Biz.instance.userInfo
        if (userInfoStr.isNotEmpty()) {
            GsonUtils.fromJson(userInfoStr, UserInfor::class.java)?.let {
                ivHead.loadCircleImage(it.headPic, R.mipmap.image_default_round)
                tvNickName.text = it.nickName
            }
        }

        val relativeLayout = LayoutInflater.from(context).inflate(R.layout.shanyan_bind_other_layout, null) as RelativeLayout
        val layoutParamsOther = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT)
        layoutParamsOther.setMargins(0, AbScreenUtils.dp2px(context, 360f), 0, 0)
        layoutParamsOther.addRule(RelativeLayout.CENTER_HORIZONTAL)
        relativeLayout.layoutParams = layoutParamsOther
        val tvOtherPhone = relativeLayout.findViewById<TextView>(R.id.tvOtherPhone)
        tvOtherPhone.setOnClickListener {
            val intent = Intent(context, BindPhoneByCodeActivity::class.java)
            ActivityUtils.startActivity(intent)
        }
        val userAgreementUrl = AppConfigManager.getInstance().configSP.getString(ConfigKeyConstant.userAgreementUrl, "")
        val privateAgreementUrl = AppConfigManager.getInstance().configSP.getString(ConfigKeyConstant.privateAgreementUrl, "")
        return ShanYanUIConfig.Builder() //                .setLightColor(true)
                .setActivityTranslateAnim("shanyan_demo_fade_in_anim", "shanyan_dmeo_fade_out_anim") //授权页导航栏：
                .setNavColor(Color.parseColor("#222222")) //设置导航栏颜色
                .setNavText("") //设置导航栏标题文字
                .setNavTextSize(18)
                .setNavTextColor(Color.parseColor("#222222")) //                .setAuthBGImgPath(backgruond)
                .setLogoHidden(true) //是否隐藏logo
                .setDialogDimAmount(0f)
                .setAuthNavHidden(true)
//                .setNavReturnImgPath(returnBg)
//                .setNavReturnBtnWidth(15)
//                .setNavReturnBtnHeight(15)
//                .setNavReturnBtnOffsetX(15)
//                .setNavReturnBtnOffsetY(10)
                .setFullScreen(false)
                .setStatusBarHidden(false) //                .setStatusBarColor(Color.parseColor(color))
                //授权页号码栏：
                .setNumberColor(Color.parseColor("#222222")) //设置手机号码字体颜色
                //                .setNumFieldOffsetBottomY(250)    //设置号码栏相对于标题栏下边缘y偏移
                //                .setNumFieldOffsetX(250)
                .setNumberSize(24)
                .setNumFieldHeight(50) //                .setNumFieldOffsetX(155)
                .setNumFieldOffsetY(230)
                .setSloganHidden(false)
                .setSloganTextSize(12)
                .setSloganOffsetY(290) //授权页登录按钮：
                .setLogBtnText("一键绑定") //设置登录按钮文字
                .setLogBtnTextColor(-0x1) //设置登录按钮文字颜色
                .setLogBtnImgPath(logBtnImgPath) //设置登录按钮图片
                .setLogBtnTextSize(15)
                .setLogBtnHeight(45)
                .setLogBtnOffsetY(315)
                .setLogBtnWidth(AbScreenUtils.getScreenWidth(context, true) - 60) //授权页隐私栏：
                .setAppPrivacyOne("用户协议", userAgreementUrl) //设置开发者隐私条款1名称和URL(名称，url)
                .setAppPrivacyTwo("隐私政策", privateAgreementUrl) //设置开发者隐私条款2名称和URL(名称，url)
                .setAppPrivacyColor(Color.parseColor("#ABABAB"), Color.parseColor("#007DCD")) //	设置隐私条款名称颜色(基础文字颜色，协议文字颜色)
                .setPrivacyText("同意", "和", "、", "、", "并授权" + context.getString(R.string.app_name) + "获取手机号")
                .setPrivacyOffsetBottomY(15) //设置隐私条款相对于屏幕下边缘y偏
                .setPrivacyState(true)
                .setPrivacyTextSize(12)
                .setPrivacyOffsetX(26)
                .setCheckBoxHidden(true)
                .setShanYanSloganHidden(true)
                .setShanYanSloganTextColor(Color.parseColor("#222222"))
                .addCustomView(mCommonTitle, false, false, null)
                .addCustomView(headLayout, false, false, null)
                .addCustomView(relativeLayout, false, false, null)
                .build()
    }

}